package com.ruoyi.web.controller.gasoline;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ChildGasoline;
import com.ruoyi.domain.GasolineExamine;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IChildGasolineService;
import com.ruoyi.service.IGasolineExamineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 【用油申请业务】Controller
 * 
 * @author ruoyi
 * @date 2020-06-23
 */
@Controller
@RequestMapping("/ruoyi/childcardboss")
public class ChildGasolineBossController extends BaseController
{
    private String prefix = "ruoyi/Childgasoline";

    @Autowired
    private IChildGasolineService childGasolineService;
    @Autowired
    private IGasolineExamineService gasolineExamineService;

    @RequiresPermissions("ruoyi:childcardlead:view")
    @GetMapping()
    public String gasoline()
    {
        return prefix + "/ChildgasolineBoss";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:childcardlead:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChildGasoline childGasoline)
    {
        childGasoline.setPeople(ShiroUtils.getLoginName());
        List<ChildGasoline> list = childGasolineService.selectChildGasolineList(childGasoline);
        list.removeIf(childGasoline1 -> childGasoline1.getcStatus()==0);
        list.removeIf(childGasoline1 -> childGasoline1.getcStatus()==1);
        list.removeIf(childGasoline1 -> childGasoline1.getcStatus()==5);
        startPage();
        return getDataTable(list);
    }

    /**
     * 通过申请
     */
    @RequiresPermissions("ruoyi:childcardlead:consent")
    @Log(title = "用油申请业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/consent")
    @ResponseBody
    public AjaxResult consent(Long id, ChildGasoline childGasoline) {
        childGasoline.setChId(id);
        childGasoline.setcStatus(2L);
        int leave = childGasolineService.updateChildGasoline(childGasoline);
        if(leave==1){
          ChildGasoline childGasoline1=  childGasolineService.selectChildGasolineById(id);
            GasolineExamine gasolineExamine=new GasolineExamine();
            GasolineExamine  gasolineExamine1=gasolineExamineService.selectGasolineExamineByeChildcard(childGasoline1.getChNumber());
            if(gasolineExamine1.geteMoney().compareTo(BigDecimal.ZERO)>0){
                gasolineExamine.seteId(gasolineExamine1.geteId());
                gasolineExamine.seteChildcard(childGasoline1.getChNumber());
                gasolineExamine.seteMoney(childGasoline1.getChTopup().add(gasolineExamine1.geteMoney()));
                gasolineExamineService.updateGasolineExamine(gasolineExamine);
            }else{
                gasolineExamine.seteId(gasolineExamine1.geteId());
                gasolineExamine.seteChildcard(childGasoline1.getChNumber());
                gasolineExamine.seteMoney(childGasoline1.getChTopup());
                gasolineExamineService.updateGasolineExamine(gasolineExamine);
            }
            return success("成功");
        }else{
            return error("失败");
        }

    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:childcardlead:export")
    @Log(title = "【用油申请业务】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChildGasoline childGasoline)
    {
        List<ChildGasoline> list = childGasolineService.selectChildGasolineList(childGasoline);
        ExcelUtil<ChildGasoline> util = new ExcelUtil<ChildGasoline>(ChildGasoline.class);
        return util.exportExcel(list, "Childgasoline");
    }


    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{chId}")
    public String edit(@PathVariable("chId") Long chId, ModelMap mmap)
    {
        ChildGasoline childGasoline = childGasolineService.selectChildGasolineById(chId);
        mmap.put("childGasoline", childGasoline);
        return prefix + "/editlead";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:childcardlead:edit")
    @Log(title = "【用油申请业务】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChildGasoline childGasoline)
    {
        childGasoline.setcStatus(3L);
        return toAjax(childGasolineService.updateChildGasoline(childGasoline));
    }


    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:childcardlead:remove")
    @Log(title = "【用油申请业务】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(childGasolineService.deleteChildGasolineByIds(ids));
    }
}
